#include "keyinputwidget.h"
#include "ui_keyinputwidget.h"

#include <QRegExpValidator>
#include <QStyle>
#include "src/utils/log.h"

KeyinputWidget::KeyinputWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::KeyinputWidget)
{
    ui->setupUi(this);

    connect(ui->pushButton_eye, &QPushButton::clicked, this, &KeyinputWidget::onEyeClicked);
    connect(ui->pushButton_enter, &QPushButton::clicked, this, &KeyinputWidget::onEnterClicked);
    connect(ui->lineEdit, &QLineEdit::returnPressed, this, &KeyinputWidget::onEnterClicked);

    ui->pushButton_eye->setProperty("eyestate", "eyeclose");
    ui->pushButton_eye->style()->polish(ui->pushButton_eye);
    ui->lineEdit->setPlaceholderText("请输入密码");
    ui->lineEdit->setEchoMode(QLineEdit::Password);
//    ui->lineEdit->installEventFilter(this);

    // 密码支持：数字0-9，英文A-Z、a-z、特殊字符（如 !, @, #, $, %, ^, &, *, (, ), -, _, =, [, ], {, }, |, ;, :, ', ", ,, ., <, >, ?）
    // 最短1个字符，最长26个字符
    // 需要转义的字符 * . ? + ^ $ | \ / [ ] ( ) { }
    QRegExp rx("[0-9a-zA-Z!@#\\$%\\^&\\*\\(\\)-_=\\[\\]\\{\\}\\|;:'\",\\.<>\\?]{1,26}");
    QRegExpValidator *validator = new QRegExpValidator(rx, this);
    ui->lineEdit->setValidator(validator);

}

KeyinputWidget::~KeyinputWidget()
{
    delete ui;
}

QString KeyinputWidget::getKeyinputString()
{
    return ui->lineEdit->text();
}

void KeyinputWidget::clearKeyinputString()
{
    ui->lineEdit->clear();
}

void KeyinputWidget::onEyeClicked()
{
    if (mPwdVisible)
    {
        ui->lineEdit->setEchoMode(QLineEdit::Password);
        ui->pushButton_eye->setProperty("eyestate", "eyeclose");
        mPwdVisible = false;
    }
    else
    {
        ui->lineEdit->setEchoMode(QLineEdit::Normal);
        ui->pushButton_eye->setProperty("eyestate", "eyeopen");
        mPwdVisible = true;
    }

    ui->pushButton_eye->style()->polish(ui->pushButton_eye);
    update();
}

void KeyinputWidget::onEnterClicked()
{
    QRegExp rx("[0-9a-zA-Z!@#\\$%\\^&\\*\\(\\)-_=\\[\\]\\{\\}\\|;:'\",\\.<>\\?]{1,26}");
    QRegExpValidator *validator = new QRegExpValidator(rx, this);
    Q_ASSERT(rx.isValid());

    QString input = getKeyinputString();
    int pos = 0;
    if (validator->validate(input, pos) == QValidator::Acceptable)
    {
        emit sigKeyEnter(input);
    }
    else
    {
        // @todo
    }
}

void KeyinputWidget::onKeytextChanged()
{

}

void KeyinputWidget::setKeyinputLocked(bool locked)
{
    if (locked)
    {
        ui->lineEdit->setEchoMode(QLineEdit::Normal);
        ui->lineEdit->setText("请输入密码");
        ui->lineEdit->setEnabled(false);
        ui->pushButton_eye->setVisible(false);
        ui->pushButton_enter->setEnabled(false);
    }
    else
    {
        ui->lineEdit->clear();
        ui->lineEdit->setEchoMode(mPwdVisible ? QLineEdit::Normal : QLineEdit::Password);
        ui->lineEdit->setEnabled(true);
        ui->pushButton_eye->setVisible(true);
        ui->pushButton_enter->setEnabled(true);
    }
    update();
}

void KeyinputWidget::setKeyinputLoading(bool isloadingAnimation)
{

}

bool KeyinputWidget::eventFilter(QObject* watched, QEvent* event)
{

    if (watched == ui->lineEdit)
    {

    }

    return QWidget::eventFilter(watched, event);
}
